﻿$(function () {
    $('a.enablebutton').click(EnableGroup);
    $('a.hotbutton').click(SetHot);
    $('a.recbutton').click(SetRec);
});

function SetRec() {
    var isRec = $(this).attr('rec') == "0" ? false : true;
    var $button = $(this);
    var $backup = $button.clone();
    isRec = !isRec;
    $button.find('img.recicon').attr('src', (relRootPath + "/Content/images/icons/") + (isRec ? "star_on.png" : "star_off.png"));
    $button.attr('title', isRec ? "取消精华" : "设置精华");
    $button.attr('alt', isRec ? "取消精华" : "设置精华");
    $button.attr('rec', isRec ? 1 : 0);

    $button.unbind('click');
    $.post(
        relRootPath + "/Mod/Groups/GroupWebService.asmx/SetRec",
        {
            groupId: $(this).attr("gid"),
            isRec: isRec
        },
        function (x, h, r) {
            $button.click(SetRec);
            if (h != "success") {
                alert("网络错误");
                $button.replaceWith($backup.click(SetRec));
            }
        }
    );
}

function SetHot() {
    var isHot = $(this).attr('hot') == "0" ? false : true;
    var $button = $(this);
    var $backup = $button.clone();
    isHot = !isHot;
    $button.find('img.hoticon').attr('src', (relRootPath + "/Content/images/icons/") + (isHot ? "fire_on.png" : "fire_off.png"));
    $button.attr('title', isHot ? "取消热门" : "设置热门");
    $button.attr('alt', isHot ? "取消热门" : "设置热门");
    $button.attr('hot', isHot ? 1 : 0);

    $button.unbind('click');
    $.post(
        relRootPath + "/Mod/Groups/GroupWebService.asmx/SetHot",
        {
            groupId: $(this).attr("gid"),
            isHot: isHot
        },
        function (x, h, r) {
            $button.click(SetHot);
            if (h != "success") {
                alert("网络错误");
                $button.replaceWith($backup.click(SetHot));
            }
        }
    );
}

function EnableGroup() {
    var enabled = $(this).attr('enabled') == "0" ? false : true;
    var $button = $(this);
    var $backup = $button.clone();
    enabled = !enabled;
    $button.find('img.enableicon').attr('src', (relRootPath + "/Content/images/icons/") + (enabled ? "block_16_g.png" : "block_16.png"));
    $button.attr('title', enabled ? "禁用圈子" : "取消禁用");
    $button.attr('alt', enabled ? "禁用圈子" : "取消禁用");
    $button.attr('enabled', enabled ? 1 : 0);

    $button.unbind('click');
    $.post(
        relRootPath + "/Mod/Groups/GroupWebService.asmx/EnableGroup",
        {
            groupId: $(this).attr("gid"),
            enabled: enabled
        },
        function (x, h, r) {
            $button.click(EnableGroup);
            if (h != "success") {
                alert("网络错误");
                $button.replaceWith($backup.click(EnableGroup));
            }
        }
    );
}